<!DOCTYPE html>
<html>
<head lang="en">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/>
    <link rel="shortcut icon" href="img/smlogo.png" type="image/x-icon">
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="css/index2.css"/>
    <!--环信-->
    <script type="text/javascript" src="sdk/webim.config.js"></script>
    <script src="sdk/strophe-1.2.8.min.js"></script>
    <script src="https://downloads.easemob.com/downloads/cdn/websdk-1.4.5.min.js"></script>
    <style>
        .user{
            height:100px;
            max-width:1000px;
            margin:64px auto 0;
            background: #fff;
            padding:14px;
            box-sizing: border-box;
            overflow: hidden;
        }
        .userimg{
            width:73px;
            height:73px;
            margin-right:26px;
        }
        .userimg>img{
            height: 72px;
            width: 72px;
        }
        .sex1,.sex2{
            display:none;
            width:13px;
            height:13px;
        }
        p.nick{
            overflow:hidden;
            font-size: 18px;
            color:#000;
        }
        p .nickword{padding-right:10px}
        p.loca{
            font-size:13px;
            color:#6a6a6a;
            margin-top:5px;
        }
        #addfribtn{
            height:55px;
            max-width:380px;
            width:85%;
            margin:0 auto;
            border:1px solid #1F9719;
            border-radius:5px;
            background:#1AAC19;
            color:#FFF;
            line-height: 55px;
            text-align: center;
            font-size:18px;
            margin-top:20px;
        }
        #addfribtn.not{
            border:1px solid #cdcdcd;
            background:#cdcdcd;
        }
        /*加好友完成提醒*/
        #sendfriendinfook{
            z-index:1000;
            width:100%;
            max-width:1000px;
            position:fixed;
            top:50%;
            transform: translateY(-50%);
            -moz-transform:translateY(-50%) ;
            -ms-transform:translateY(-50%) ;
            -o-transform:translateY(-50%) ;
            -webkit-transform:translateY(-50%) ;
        }
        #sendfriendinfook div{
            width:60%;
            max-width:220px;
            margin:0 auto;
            min-height:80px;
            /*line-height: 80px;*/
            text-align: center;
            font-size: 18px;
            color: #fff;
            background: rgba(53,53,53,0.5);
            overflow: hidden;
            border-radius: 4px;
            padding-top: 26px;
        }
        .errer{
            text-align: center;
            margin-top:15px;
            font-size: 13px;
            color:#b92b27;;
            height:18px;
        }
        .header_main{
            line-height:44px;
            color: rgba(255,255,255,0.85);
            font-size:16px;
        }
    </style>
</head>
<body>
<div class="Header">
    <div class="fixed_header">
        <div class="header_main">
            <a class="logo symbol_logo" id="logo"></a>
            <span>添加好友</span>
        </div>
    </div>
</div>
<div class="user">
    <div class="userimg lf">
        <img src=""/>
    </div>
    <div class="lf">
        <p class="nick">
            <span class="nickword"></span>
            <img src="img/man.png" alt="" class="sex1"/>
            <img src="img/woman.png" alt="" class="sex2"/>
        </p>
        <p class="loca"></p>
    </div>
</div>
<div id="addfribtn">添加好友</div>
<!-- 提醒-->
<p class="errer"></p>
<!--加好友完成提醒-->
<div id="sendfriendinfook" style="display:none;">
    <div>
        <span>请求已发送</span> <br/><span>自动跳转至首页</span>

    </div>
</div>
<script src="js/jquery-1.11.3.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

<script src="ftnjs/urlnote.js"></script>
<script>
    //扫码者 wx登录
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        //没有用户ID
        if(!$.cookie("MYID")){
            //修改地址中拼接的微信地址
            //从微信验证返回后获取openid
            if(/code=/.test(location.href)){
                var reg=new RegExp("code=[a-zA-Z0-9]*");
                var code=location.href.match(reg)[0];

                //立即获取openid 时效性
                $.ajax({
                    type:"GET",
                    url:"GetOpenId.ashx?"+code,
                    async: false,
                    success:function(data){

                        var dat=data;
                        //var openid=dat.openid;
                        //保存到本地后 进行重定向
                        $.cookie("wxopenid",dat);
                        $.cookie("myopenid",dat);
                        //拼接的fromuser先赋值为原始的转发者
                        var fromuser= $.cookie("fromuser");
                        //openid如果存在 已经关注使用了 获取自身userID
                        if(dat){
                            $.ajax({
                                type:"GET",
                                url:getwxopenidurl+$.cookie("myopenid"),
                                success:function(d){
                                    //能拿的到用户信息 //已经登录
                                    if(d.Data&&d.Data.userId.length>0){
                                        thisnick=d.Data.userNick;
                                        id=d.Data.userId;
                                        $.cookie("MYID",d.Data.userId);
                                        //修改地址 链接环信
//
                                        location.href="qrcodeuser.html#id="+$.cookie("fromuser");
                                    }
                                    //拿不到用户数据
                                    else{
                                        $(".errer").html("您的用户信息不全请登录铺路后，扫码添加好友");
                                        getqrnote(fromuser,fromuser);
                                    }
                                }
                            });
                        }
                        else{
                            $(".errer").html("您的用户信息不全请登录铺路后，扫码添加好友");
                            getqrnote(fromuser,fromuser);
                        }
                    }
                })
            }
            //页面没有微信验证信息 //页面第一次进来进行微信验证
            else{
                var loca=location.href;
                var fromuser=(location.href).match(/id=[0-9a-z]+/)[0].split("=")[1];
//                    if($.cookie("fromuser")&&$.cookie("fromuser")==fromuser){ //验证后没有获取用户信息的跳转
//
//                    }else if(!$.cookie("fromuser")){
//
//                    }
                $.cookie("fromuser",fromuser);
                //先微信验证获取openid 如果已经授权过了 直接页面截取openid 然后发送请求获取 用户的ID
                var reg=new RegExp("code=[a-zA-Z0-9]*");
                var returnurl2="http://www.pulu.com/weixin/qrcodeuser.html";
                location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx658c34793ecfd9f9&redirect_uri="+returnurl2+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";

            }
        }
        //有用户ID 
        else{

            var loca=location.href;
            if(/id=[0-9a-z]/.test(loca)){
                var fromuser=(location.href).match(/id=[0-9a-z]+/)[0].split("=")[1];
            }
            else if($.cookie("fromuser")){
                var fromuser=$.cookie("fromuser");
            }
            else{
                var fromuser=$.cookie("MYID");
            }
            id=$.cookie("MYID");
            //环信建立连接
            var conn = new WebIM.connection({
                https: WebIM.config.https,
                url: WebIM.config.xmppURL,
                isAutoLogin: WebIM.config.isAutoLogin,
                isMultiLoginSessions: WebIM.config.isMultiLoginSessions
            });
            //登录
            var options = {
                apiUrl: WebIM.config.apiURL,
                user:$.cookie("MYID"),
                pwd:$.cookie("MYID"),
                appKey: "pulu#pulu"
            };
            conn.open(options);
            if($.cookie("MYID")==fromuser){$(".errer").html("不能添加自己")};
            getqrnote($.cookie("MYID"),fromuser);
        }
    }
    else{
        var loca=location.href;
        if(/id=[0-9a-z]+/.test(loca)){
            var fromuser=(location.href).match(/id=[0-9a-z]+/)[0].split("=")[1];
        }
        if($.cookie("MYID")){
            if($.cookie("MYID")==fromuser){$(".errer").html("不能添加自己")};
            getqrnote($.cookie("MYID"),fromuser);
        }
        else{
            $(".errer").html("您的用户信息不全请登录铺路后，扫码添加好友");
            getqrnote(fromuser,fromuser);
        }
    }
    var canadd=true;

    //名片信息
    function getqrnote(myid,fromuser){

        if(myid==fromuser){
            $("#addfribtn").attr("class","not");
//                $(".errer").html("不能添加自己");
        }
        if(/undefined/.test(myid)&&/undefined/.test(fromuser)){
            //全部错误
        }else if(/undefined/.test(myid)&&!/undefined/.test(fromuser)){
            myid=fromuser;
        }else if(!/undefined/.test(myid)&&/undefined/.test(fromuser)){
            fromuser=myid;
        }

        $.ajax({
            type: "post",
            url: userurl,
            data: {"loginuserId": myid,"userId":fromuser},
            datatype: "json",
            success: function (data) {
                $(".nickword").html(data.Data.userNick);
                if(data.Data.userAva){
                    $(".userimg img").attr("src","http://42.62.66.198:8003/"+data.Data.userAva);
                }else{
                    $(".userimg img").attr("src","img/af8.png");
                }
                if(data.Data.userSex===1){$(".sex1").css("display","inline-block");}else if(data.Data.userSex===0){$(".sex2").css("display","inline-block");}
                if(data.Data.locationCity&&data.Data.locationCity.length>0&&data.Data.locationCity!="null"){
                    $(".loca").html(data.Data.locationCity);
                }
                if(data.Data.isFriend){
                    $("#addfribtn").attr("class","not");
                    $(".errer").html("已经是好友");
                }
            }
        })
    }
    //加好友
    $("#addfribtn").click(function(){
        if($.cookie("MYID")){
            if($("#addfribtn").attr("class")!="not"&&canadd){
                id=$.cookie("MYID");
                console.log(id);
                if(fromuser!= $.cookie("MYID")){
                    conn.subscribe({
                        to: fromuser,
                        message:"申请添加好友"
                    });
                    canadd=false;
                    $("#sendfriendinfook").css("display","block");
                    setTimeout(function(){
                        $("#sendfriendinfook").css("display","none");
                        location.href="http://www.pulu.com/weixin/pulu.html";
                    },1000)
                }
            }
        }
        else{
            location.href="http://www.pulu.com/weixin/index.html?fromuser="+id;
        }
    });
    //返回主页
    $("#logo").click(function(){
        location.href="http://www.pulu.com/weixin/pulu.html";
    })
</script>
</body>
</html>